package org.tp.framework.container;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tp.framework.constant.ExceptionConstant;
import org.tp.framework.exception.TPFrameworkException;
import org.tp.framework.mapping.Mapping;
import org.tp.framework.util.StringUtils;

/**
 * mvc 映射容器
 * 
 * @author thinstop
 *
 */
public class MvcMappingContainer {

	private static Logger logger = LoggerFactory.getLogger(MvcMappingContainer.class);
	private static Map<String, Mapping> mappingContainer = new ConcurrentHashMap<>();

	public static void addMapping(Mapping mapping) {
		if (mappingContainer.containsKey(mapping.getUrl())) {
			logger.error("MvcMappingContainer addMapping url exist:{}", mapping.getUrl());
			throw new TPFrameworkException(ExceptionConstant.MVC_PATH_EXIST_ERROR_CODE,
					ExceptionConstant.MVC_PATH_EXIST_ERROR_MSG);
		}
		mappingContainer.put(mapping.getUrl(), mapping);
	}

	public static boolean containUrl(String url) {

		return mappingContainer.containsKey(url);
	}

	public static Mapping getMapping(String mappingUrl) {
		if (StringUtils.isEmpty(mappingUrl)) {
			return null;
		}
		return mappingContainer.get(mappingUrl);
	}

}
